﻿using DB.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi_For_Auth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        // GET: api/<DepartmentController>
        [HttpGet]
        public List<Department> Get()
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.Departments.ToList();
                return data;
            }
        }

        // GET api/<DepartmentController>/5
        [HttpGet("{id}")]
        public Department Get(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.Departments.Where(o => o.Id == id).SingleOrDefault();
                return data;
            }
        }

        // POST api/<DepartmentController>
        [HttpPost]
        public void Post([FromBody] Department value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                db.Departments.Add(value);
                db.SaveChanges();
            }
        }

        // PUT api/<DepartmentController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] Department value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var department = db.Departments.Where(o => o.Id == value.Id).SingleOrDefault();
                if (department != null)
                {
                    department.Name = value.Name;
                    department.Description = value.Description;
                    department.UpdateTime = DateTime.Now;
                    db.SaveChanges();
                }
            }
        }

        // DELETE api/<DepartmentController>/5
        [HttpDelete("{id}")]
        public void Delete(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var department = db.Departments.Where(o => o.Id == id).SingleOrDefault();
                if (department != null)
                {
                    db.Departments.Remove(department);
                    db.SaveChanges();
                }
            }
        }

        /// <summary>
        /// Updates the path of all departments in the database.
        /// 这个方法尚未验证，但是感觉应该使用递归来进行处理。
        /// 这个方法不正确，待完善。2025年6月25日14:24:33
        /// </summary>
        private void UpdateDepartmentPath()
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var departments = db.Departments.ToList();
                foreach (var department in departments)
                {
                    if (string.IsNullOrEmpty(department.Path))
                    {
                        department.Path = department.Id;
                    }
                    else
                    {
                        department.Path += $"/{department.Id}";
                    }
                }
                db.SaveChanges();
            }
        }
    }
}
